package gov.tax.rti.base.controller;

import gov.tax.rti.base.service.impl.DataMapServiceImpl;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.bstek.bdf2.core.orm.DataSourceRepository;
import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.Expose;
import com.bstek.dorado.core.Configure;

@Transactional(propagation=Propagation.REQUIRED,readOnly=true)
@Component("gdgs.base.dataMapInterceptor")
public class DataMapInterceptor implements ApplicationContextAware{
	
	@Resource
	private DataMapServiceImpl dataMapServiceImpl;
	
	private Map<String,String> dataSourceMap;
	
	@Expose
	public List<Map<Object,Object>> getMapping(String sqlKey) {
		return dataMapServiceImpl.getMapping(sqlKey);
	}
	
	@Expose
	public List<Map<Object,Object>> getJoinMapping(String sqlKey,Map<String,Object> paras) {
		return dataMapServiceImpl.getJoinMapping(sqlKey, paras);
	}

	@DataProvider
	public Map<String,String> getDataSourceMap(){
		return this.dataSourceMap;
	}

	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException{
		this.dataSourceMap = new HashMap<String, String>();
		this.dataSourceMap.put("", "默认数据源");
		DataSourceRepository dataSourceRepository = null;
		Collection<DataSourceRepository> dataSourceRepositoryCollection=applicationContext.getBeansOfType(DataSourceRepository.class).values();
		if(dataSourceRepositoryCollection.size()>0){
			dataSourceRepository=dataSourceRepositoryCollection.iterator().next();			
		}else if(applicationContext.getParent()!=null){
			dataSourceRepositoryCollection=applicationContext.getParent().getBeansOfType(DataSourceRepository.class).values();
			if(dataSourceRepositoryCollection.size()>0){
				dataSourceRepository=dataSourceRepositoryCollection.iterator().next();		
			}
		}
		if(dataSourceRepository != null){
			String defaultDataSourceName = dataSourceRepository.getDefaultDataSourceName();
			if(StringUtils.isNotEmpty(defaultDataSourceName)){
				this.dataSourceMap.put(defaultDataSourceName, getDataSourceLabel(defaultDataSourceName) + "(默认数据源)");
			}
			for (String dataSourceName : dataSourceRepository.getDataSources().keySet()) {
				if(!dataSourceName.equals(defaultDataSourceName)){
					this.dataSourceMap.put(dataSourceName, getDataSourceLabel(dataSourceName));
				}
			}
		}
		
	}
	
	protected String getDataSourceLabel(String dataSourceName){
		return Configure.getString("rti.dataSourceLabel."+dataSourceName, dataSourceName);
	}

}
